<?php

class FileController extends CoreController
{
    public $img;
    public $file;
    public function init(){
        Yii::import('application.vendor.*');
        require_once('Tp/ThinkUpload/Upload.class.php');
        require_once('Tp/ThinkImage/Image.class.php');
    }

    //上传微博图集的
    public function ActionUpload()
    {
        $upload = new Upload();
        $dir = Yii::app()->params['uploadPath'].'wb/'.date('Y',time()).'/'.date('m',time()).'/';
        if(!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        $upload->rootPath = $dir;
        $upload->maxSize = 1048579;
        $info = $upload->upload();
        if ($info) {
            //原图
            $imgPath = $dir . $info['Filedata']['savepath'] . $info['Filedata']['savename'];
            //缩略图
            $thumbPath = $dir . $info['Filedata']['savepath'] . '180_' . $info['Filedata']['savename'];
            //放大图
            $unfoldPath = $dir . $info['Filedata']['savepath'] . '550_' . $info['Filedata']['savename'];
            $image = new Image();
            $image->open($imgPath);
            $image->thumb(180, 180)->save($thumbPath);
            $image->open($imgPath);
            $image->thumb(550, 550)->save($unfoldPath);
            $imageArr = array(
                'thumb' => $thumbPath,
                'unfold' => $unfoldPath,
                'source' => $imgPath,
            );
            echo Xtutuquan::ajaxReturn($imageArr);
        } else {
            echo $upload->getError();
        }
    }

    //裁剪并保存背景
    public function actionCropImg(){
        if (Yii::app()->request->isAjaxRequest) {
            //图片裁剪数据
            if(!isset($_POST) && empty($_POST)){
                return;
            }
            //头像目录
            //子目录
            $subName = date('Y-m',time());
            $path = Yii::app()->params['uploadPath'].'bg/'.$subName.'/';
            //要保存的图片
            $real_path = $path.Yii::app()->request->getParam('picName');
            //临时图片地址
            $pic_path = $path.Yii::app()->request->getParam('picName');
            $image = new Image();
            //IMAGE_THUMB_FIXED
            $image->open($pic_path)->crop(Yii::app()->request->getParam('w'),Yii::app()->request->getParam('h'),Yii::app()->request->getParam('x'),Yii::app()->request->getParam('y'),400,160);
            $image->save($real_path);
            //修改数据库背景字段
            $data['status'] = 0;
            if(Yii::app()->user->id){
                $user_info =User::model()->findByPk(Yii::app()->user->id);
                if($user_info){
                    $auth = Yii::app()->user->getState('user_auth');
                    //写入数据库
                    $user_info->updateByPk(Yii::app()->user->id,array('bg'=>$real_path));
                    //更改auth状态
                    $auth['bg_url'] = $real_path;
                    Yii::app()->user->setState('user_auth', $auth);
                    $data['bg_url'] = Yii::app()->params['imgPath'].$real_path;
                    $data['status'] = 1;
                }
            }
            echo Xtutuquan::ajaxReturn($data);
        }else{
            Xtutuquan::redirect_message('非法操作', 'error', 2);
        }
    }


    //上传背景uploadBg
    public function ActionUploadBg(){
        $upload = new Upload();
        $upload->maxSize = 3*1024*1024;
        $upload->exts = array('jpg','png','gif','jpeg');
        $dir = Yii::app()->params['uploadPath'].'bg/';
        $upload->rootPath = $dir;
        $upload->replace = true;
        //完整的背景内容
        $upload->subName = array('date', 'Y-m');
        $info = $upload->upload();
        if(!$info){
            echo $upload->getError();
        }else{
            $path_url = $dir.$info['Filedata']['savepath'].$info['Filedata']['savename'];
            $temp_size = getimagesize($path_url);
            if($temp_size[0] < 400 || $temp_size[1]<160){
                $data['status'] = 0;
                $data['message']='图片不得小于400*160';
                echo Xtutuquan::ajaxReturn($data);
                Yii::app()->end();
            }
            $data['picName'] = $info['Filedata']['savename'];
            $data['url'] = Yii::app()->params['imgPath'].$path_url;
            $data['status'] = 1;
            echo Xtutuquan::ajaxReturn($data);
            Yii::app()->end();
        }
    }


    //flash上传头像
    public function actionUploadAvatar(){
        $result = array();
        $result['success'] = false;
        if(!Yii::app()->user->id){
            $result['msg'] = '非法请求';
            echo json_encode($result);
            Yii::app()->end();
        }
        $successNum = 0;
        $msg = '';
        $dir1 = ceil(Yii::app()->user->id / 10000);
        $dir2 = ceil(Yii::app()->user->id % 10000 / 1000);
        $upload = Yii::app()->params['uploadPath'];
        $dir = $upload.'avatar/'.$dir1.'/'.$dir2.'/'.Yii::app()->user->id.'/';
        if(!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        foreach($_FILES  as $key=>$val){
            if($_FILES[$key]['error'] > 0){
                $msg .= $_FILES[$key]['error'];
            }else{
                switch($key){
                    case '__avatar1':
                        $dir_url = $dir.'200x200.jpg';
                        break;
                    case '__avatar2':
                        $dir_url = $dir.'100x100.jpg';
                        break;
                    case '__avatar3':
                        $dir_url = $dir.'36x36.jpg';
                        break;
                    default:
                }
                move_uploaded_file($_FILES[$key]["tmp_name"], $dir_url);
                $successNum++;
            }
        }
        $result['msg'] = $msg;
        if ($successNum > 0) {
            $auth = Yii::app()->user->getState('user_auth');
            if ($auth['avatar_status'] == 0) {
                //写入数据库
                User::model()->updateByPk(Yii::app()->user->id,array('avatar_status'=>1));
                //更改auth状态
                $auth['avatar_status'] = 1;
                Yii::app()->user->setState('user_auth', $auth);
            }
            $result['success'] = true;
        }
        echo json_encode($result);
    }
    //上传分享
    public function actionUploadShare(){
        $upload = new Upload();
        $dir = Yii::app()->params['uploadPath'].'photo/'.date('Y',time()).'/'.date('m',time()).'/';
        if(!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        $upload->rootPath = $dir;
        $upload->maxSize = 5*1024*1024;
        $upload->exts = array('jpg','png','gif','jpeg');
        $info = $upload->upload();
        if ($info) {
            $imgPath = $dir . $info['file']['savepath'] . $info['file']['savename'];
            $imageArr = array(
                'image' => $imgPath,
            );
            echo Xtutuquan::ajaxReturn($imageArr);
        } else {
            echo $upload->getError();
        }
    }


    public function actionTest1(){

    }


}